# -*- coding:utf-8 -*-

import pygame
import random
import math
import copy
from pygame.locals import *

width = 1200
height = 800

radius = 5
march_step = 2

ball_radius = 20
ball_step = 30

rim_pos = [0, width / 2]
rim_len = 30

# generate monsters on map randomly
def gen_monster():
    list_monster = []
    for x in range(1, width, 10):
        for y in range(1, height, 10):
            if random.randint(1, 200) == 1:  # 1/10 percent
                list_monster.append([x, y])
    return list_monster


def main():
    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([width, height])

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    restart = True
    while restart:
        screentitle = pygame.display.set_caption("slg map")

        ball_pos = []
        shot_pos = []

        trail_point_list = []

        trail_index = 0

        running = True
        while running:
            # fill the background is white
            screen.fill([255, 255, 255])

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                elif event.type == MOUSEMOTION:
                    if len(ball_pos) > 0:
                        pos = pygame.mouse.get_pos()
                        mouse_x = pos[0]
                        mouse_y = pos[1]
                        if mouse_x > 0 and mouse_y > 0:
                            shot_pos = [mouse_x, mouse_y]
                            # pygame.draw.line(screen, [0, 255, 0], ball_pos, [mouse_x, mouse_y], 2)
                    # if mouse_x > 0 and mouse_y > 0:
                    # pygame.draw.line(screen, [0, 255, 0], [mouse_x, mouse_y], end_pos, 2)
                elif event.type == MOUSEBUTTONDOWN:
                    pos = pygame.mouse.get_pos()
                    mouse_x = pos[0]
                    mouse_y = pos[1]
                    ball_pos = [mouse_x, mouse_y]
                    # if mouse_x > 0 and mouse_y > 0:
                        # pygame.draw.line(screen, [0, 255, 0], [mouse_x, mouse_y], end_pos, 2)
                elif event.type == MOUSEBUTTONUP:
                    pos = pygame.mouse.get_pos()
                    shot_pos = [pos[0], pos[1]]

                    k = float(shot_pos[1] - ball_pos[1]) / (shot_pos[0] - ball_pos[0])

                    b = ball_pos[0] - 2 * ball_pos[1] / float(k)
                    a = k * k / (4 * ball_pos[1])

                    for x in range(ball_pos[0], 0, -ball_step):
                        y = a * (x - b) * (x - b)
                        trail_point_list.append([x, int(round(y))])

                    ball_pos = []

            time_pass = clock.tick(10)

            rim_rect = pygame.draw.ellipse(screen, (0xCC, 0xCC, 0x00), ((0, height / 4), (100, 25)), 2)

            if len(ball_pos) > 0 and len(shot_pos) > 0:
                pygame.draw.line(screen, [0, 255, 0], ball_pos, shot_pos, 2)

            trail_rect_list = []
            if len(trail_point_list) > 0 and trail_index < len(trail_point_list):
                # pygame.draw.circle(screen, (0, 0, 255), trail_point_list[trail_index], 10)
                for index in range(0, trail_index):
                    trail_rect = pygame.draw.circle(screen, (0, 0, 255), trail_point_list[index], ball_radius)

                    if trail_rect.colliderect(rim_rect):
                        running = False

                trail_index = trail_index + 1

            # if player_rect.colliderect(end_rect):
            #     running = False
            pygame.display.update()
        # game over background
        pygame.font.init()
        screen.fill([100, 0, 0])
        font = pygame.font.Font(None, 48)
        text = font.render("Game Over !!!", True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery + 24
        screen.blit(text, textRect)
        # keydown r restart,keydown n exit
        while 1:
            event = pygame.event.poll()
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    restart = True
                    break
                if event.key == pygame.K_n:
                    restart = False
                    break
            pygame.display.update()


if __name__ == '__main__':
    main()
